$(function () {
    $(".loadingMarsk").css({
        "width": window.screen.availWidth,
        "height": window.screen.availHeight,
    });
    new FastClick(document.body);
    //获取id
    var id = tools.queryString().id || 1;
    //获取活动数据
    function loadActivityDetail(status) {
        $.ajax({
            type: "get",
            url: domain.test + "/getactivitydetail",
            // url: domain.online + "/api/activity",
            data: { id: id },
            beforeSend: function () {
                if (status === 0) {
                    $(".loadingMarsk").css("display", "block");
                }
            },
            success: function (response) {
                if (response.data.rtnCode === 200) {
                    // if(response.rtnCode===200) {
                    var activityDetail = response.data.data;
                    // var activityDetail = response.data;
                    console.log(activityDetail);
                    $(".activityBox").html(template("activityBox", { activityDetail: activityDetail }));
                    if (status === 0) {
                        $(".loadingMarsk").css("display", "none");
                    }
                    //订单列表滚动
                    var $this = $(".orderList");
                    var scrollTimer;
                    $this.hover(function () {
                        clearInterval(scrollTimer);
                    }, function () {
                        scrollTimer = setInterval(function () {
                            scrollNews($this);
                        }, 2000);
                    }).trigger("mouseout");
                    function scrollNews(obj) {
                        var $self = obj.find(".itemBox:first");
                        var lineHeight = $self.find(".orderItem:first").height();
                        $self.animate({ "margin-top": -lineHeight + "px" }, 350, function () {
                            $self.css({ "margin-top": "0px" }).find(".orderItem:first").appendTo($self);
                        })
                    }
                    if (activityDetail.second) {
                        var value = Math.abs(activityDetail.second);
                        var timer = setInterval(function () {
                            value -= 1000;
                            if (value < 1000) {
                                clearInterval(timer);
                                loadActivityDetail(1);
                            }
                            var hour = Math.floor(value / 1000 / 3600);
                            var minute = Math.floor(value / 1000 / 60 % 60);
                            var second = Math.floor(value / 1000 % 60);
                            hour = hour >= 10 ? hour : "0" + hour;
                            minute = minute >= 10 ? minute : "0" + minute;
                            second = second >= 10 ? second : "0" + second;
                            $(".hour").html(hour);
                            $(".minute").html(minute);
                            $(".second").html(second);
                        }, 1000);
                    }
                }
            }
        });
    }
    loadActivityDetail(0);
    //获取活动评论
    var acPageNum = 1;
    window.acCommentList = [];
    function getActivityComment(id, acPageNum, CommentList) {
        $.ajax({
            type: "get",
            url: domain.test + "/getactivitycomments",
            // url:domain.online + "/api/activity/review",
            data: { id: id, pageNum: acPageNum },
            success: function (data) {
                if (data.data.rtnCode === 200) {
                    // if(data.rtnCode === 200) {
                    var acCommentList = data.data.data;
                    console.log(acCommentList);
                    // var acCommentList = data.data;
                    window.acCommentList = CommentList.concat(acCommentList.list);
                    window.acMore = acCommentList.more;
                    template.helper("acMore", function () {
                        return window.acMore;
                    });
                    $(".comments").html(template("comments", { commentList: window.acCommentList }));
                }
            }
        });
    }
    getActivityComment(id, acPageNum, window.acCommentList);
    $(".comments").on("click", ".moreCommentsBtn", function () {
        if (window.acMore) {
            acPageNum++;
            getActivityComment(id, acPageNum, window.acCommentList);
        } else {
            $(this).html("没有更多评论了");
            return;
        }
    });
    //查看详情
    $(".activityBox").on("click", ".productItem", function () {
        var pID = $(this).attr("data-id");
        location.href = "../classify/product.html?id=" + pID;
    });
    //活动评论
    var pId;
    var replyName;
    $(".comments").on("click", ".writeComent", function () {
        var width = window.screen.availWidth;
        var height = window.screen.availHeight;
        $(".commentBoxMarsk").css("width", width);
        $(".commentBoxMarsk").css("height", height);
        $(".commentBoxMarsk").css("display", "block");
        pId = parseInt($(this).attr("data-id"));
    });
    $(".comments").on("click", ".replyBtn", function () {
        var width = window.screen.availWidth;
        var height = window.screen.availHeight;
        $(".commentBoxMarsk").css("width", width);
        $(".commentBoxMarsk").css("height", height);
        $(".commentBoxMarsk").css("display", "block");
        pId = parseInt($(this).attr("data-id"));
        console.log(pId);
        replyName = $(this).parent().parent().find(".rName").html();
        console.log(replyName);
        $(".commentContant").attr("placeholder","回复：" + replyName);
    })
    //取消评论
    $(".cancelBtn").on("click", function () {
        $(".commentBoxMarsk").css("display", "none");
        $(".commentContant").val("");
        $(".commentContant").attr("placeholder","请输入留言(45字)");
    });
    //提交评论
    var token = localStorage.token;
    function tips(tips) {
        $(".tips").css("display", "block").html(tips).animate({
            "opacity": 1
        }, 1000, function () {
            setTimeout(function () {
                $(".tips").animate({
                    "opacity": 0
                }, 1000, function () {
                    $(".tips").css("display", "none");
                })
            }, 1000);
        })
    }
    $(".submiteBtn").on("click", function () {
        submitComments();
    });
    function submitComments() {
        var content = $(".commentContant").val();
        if (!content) {
            tips("评论内容不能为空");
            return false;
        }
        if (content.length > 45) {
            tips("评论内容不能超过45字");
            return false;
        }
        if (!token) {
            location.href = "../loginAndReg/login.html";
            return false;
        }
        console.log(pId);
        if (pId) {
            pId = pId;
        }
        if (!pId) {
            pId = "";
        }
        var nickName = localStorage.nickname;
        var headPic = localStorage.userHeadPic;
        $.ajax({
            type: "post",
            url: domain.test + "/activitycomment",
            data: {
                activityId: id,
                content: content,
                pId: pId
            },
            beforeSend: function (xhr) {
                $(".loadingMarsk").css("backgroud-color","rgba(0,0,0.3)");
                $(".loadingMarsk").css("display","block");                
                xhr.setRequestHeader("token", token);
            },
            success: function (response) {
                console.log(response);
                if (response.data.rtnCode === 200) {
                    $(".loadingMarsk").css("display","none"); 
                    $(".commentBoxMarsk").css("display", "none");  
                    $(".commentContant").val("");
                    $(".commentContant").attr("placeholder","请输入留言(45字)");
                    if (!pId) {
                        $(".commentsContainer").prepend(`<div class="userComments">
                        <div class="userCommentItem clearfix">
                            <div class="userHeader">
                                <img src="${headPic}" alt="">
                            </div>
                            <div class="commentContainer">
                                <div class="usernameAndReply clearfix">
                                    <div class="userName">
                                        ${nickName}
                                    </div>
                                    <div class="reply"><span>回复</span></div>
                                </div>
                                <div class="content">${content}</div>
                            </div>
                        </div>
                    </div>`);
                    }
                    if(pId) {
                        $(".commentsContainer").prepend(`<div class="userComments">
                        <div class="userCommentItem clearfix">
                            <div class="userHeader">
                                <img src="${headPic}" alt="">
                            </div>
                            <div class="commentContainer">
                                <div class="usernameAndReply clearfix">
                                    <div class="userName">
                                        ${nickName}
                                        <span class="replySomeone">回复</span><span class="replyName">${replyName}</span> 
                                    </div>
                                    <div class="reply"><span class="replyBtn">回复</span></div>
                                </div>
                                <div class="content">${content}</div>
                            </div>
                        </div>
                    </div>`);
                    }
                }
            }
        });
    }
});